﻿using Flurl.Http;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            Console.WriteLine("开始...");
            Console.WriteLine();

            List<User> users = new List<User>();
            users.Add(new User { userName = "kH34Ph50XCFmeWkbhdfK8A==", password = "j0HWNufZv4B+jpGEm89KBg==", userNameKey = "ef5f295878e94d1986daa74d50e382dc", Name = "王春兰" });

            //users.Add(new User { userName = "pi2aC5nGwRYVnBNYRzCE4A==", password = "j0HWNufZv4B+jpGEm89KBg==", userNameKey = "92b60db188a84fc39b3913aaf0a788c1", Name = "孟宇航" });

            users.Add(new User { userName = "VTjk1bTBMzSe1l5b0rxDXw==", password = "j0HWNufZv4B+jpGEm89KBg==", userNameKey = "18f1a1fa65584dbe81b2700e6f4528bc", Name = "张俊国" });

            //users.Add(new User { userName = "DcYf+uklGctdyd/uuCzwdQ==", password = "j0HWNufZv4B+jpGEm89KBg==", userNameKey = "7e9e3d0531e24a5bacfc6670bd55c8a3", Name = "郭春生" });

            users.Add(new User { userName = "ZzzUEzxBAoli2A6Ymc4mjQ==", password = "j0HWNufZv4B+jpGEm89KBg==", userNameKey = "8c2c0e97af284bfea9c746fba276a174", Name = "陶娟" });

            //users.Add(new User { userName = "6OCyfVPlFgIVsRXpbCMqDA==", password = "j0HWNufZv4B+jpGEm89KBg==", userNameKey = "a57eebe730cb4270a59b07f42bd448b6", Name = "郑玉婷" });

            users.Add(new User { userName = "x7fqgTni7QCXxH4qapgd9g==", password = "j0HWNufZv4B+jpGEm89KBg==", userNameKey = "2a4e35223de44068af28c6e384fab466", Name = "王罗红" });

            users.Add(new User { userName = "k5HMv4vRdgY3MOmJMgM/wQ==", password = "j0HWNufZv4B+jpGEm89KBg==", userNameKey = "d0ce1fd3022e4a01ad5fb4c5ac604b11", Name = "潘昱飚" });

            users.Add(new User { userName = "7U7v9glLfKGvOob7lSn7tw==", password = "j0HWNufZv4B+jpGEm89KBg==", userNameKey = "ff0530a405714f9eb1ca7f93c3df6760", Name = "陈飞" });

            users.Add(new User { userName = "NO1zZ2xmS3z//H2rhnb9Ug==", password = "j0HWNufZv4B+jpGEm89KBg==", userNameKey = "9a3b96b65d25428ab95f6f7202ce56cc", Name = "尹剑" });

            users.Add(new User { userName = "740L34OBylmWslMLWK6VYA==", password = "j0HWNufZv4B+jpGEm89KBg==", userNameKey = "48a2f3bee746486fa8b43497bbd39523", Name = "冯灿" });

            //users.Add(new User { userName = "p5SodzOHRZB697xVVk1eKw==", password = "j0HWNufZv4B+jpGEm89KBg==", userNameKey = "73b0ac50be48463483ca09a87d780d3f", Name = "吴辰晔" });

            string loginUrl = "http://49.74.127.8:9035/js-rylb/j_spring_security_check";

            string impersonateUrl = "http://49.74.127.8:9035/js-rylb/login/impersonate";

            string dayStyUrl = "http://49.74.127.8:9035/js-rylb/specialController/dayStyScore";

            string logoutUrl = "http://49.74.127.8:9035/js-rylb/j_spring_security_logout";

            //string checkImgUrl = "http://49.74.127.8:9035/js-rylb/rylbApi/userCenter/checkImgCode?captcha=";

            //string ocrImgUrl = "http://127.0.0.1:12349";

            foreach (var user in users)
            {
                Console.Write(user.Name);

                //string imgurl = $"http://49.74.127.8:9035/js-rylb/random/code/newimagesrc?uuid={Guid.NewGuid()}";

                //var imgResponse = await imgurl.GetAsync().ReceiveJson<ImgData>();

                //if (imgResponse.status != "200") Console.WriteLine("获取验证码失败");


                //登录
                var response = await loginUrl.WithHeader("Accept", "application/json")
                    .WithHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36 Edg/105.0.1343.53")
                    .PostUrlEncodedAsync(new { username = user.userName, password = user.password });

                var headers = response.Headers;
                var loginData = await response.GetJsonAsync<TokenData>();

                var token = loginData.data.data.token;

                headers.TryGetFirst("Set-Cookie", out string value);

                var cookie = value.Remove(0, 8);

                //再次获取新的token
                var tokenData = await impersonateUrl.WithCookie("SESSION", cookie)
                                            .WithHeader("X-CSRF-TOKEN", token)
                                            .WithHeader("Accept", "application/json")
                                            .WithHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36 Edg/105.0.1343.53")
                                            .PostUrlEncodedAsync(new { username = user.userNameKey })
                                            .ReceiveJson<TokenData>();

                token = tokenData.data.data.token;

                //提交试卷
                var styResponse = await dayStyUrl.WithCookie("SESSION", cookie)
                                               .WithHeader("X-CSRF-TOKEN", token)
                                            .WithHeader("Accept", "application/json")
                                            .WithHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36 Edg/105.0.1343.53")
                                            .PostUrlEncodedAsync(new { trueNum = 10, errorNum = 0 })
                                            .ReceiveJson<ResultData>();

                if (styResponse.code == 200)
                {
                    var space = "      ";
                    if (user.Name.Length == 2) space += "  ";
                    Console.WriteLine($"{space}完成");
                }

                ////退出
                //await logoutUrl.WithCookie("SESSION", cookie)
                //    .WithHeader("X-CSRF-TOKEN", token)
                //    .WithHeader("Accept", "application/json")
                //    .WithHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36 Edg/105.0.1343.53")
                //    .PostAsync();
            }

            Console.WriteLine();
            Console.WriteLine("结束1111");
            Console.ReadKey();
        }
    }
}
